#!/usr/bin/perl

my $input=$ARGV[0];

my $level;
if ( "x$input" eq "xINFO" ){
	$level="INFO";
}elsif( "x$input" eq "xDEBUG" ) {
	$level="DEBUG";
}else{
	print "$0 INFO|DEBUG\n";
	print "you need to restart jboss after the change!\n";
	exit;
}

my $jbosslogfile=$ENV{JBOSS_HOME} . "/server/default/conf/jboss-log4j.xml";
my $jbosslogfileorg="$jbosslogfile" . ".org";
my $jbosslogfilenew="$jbosslogfile" . ".new";

if ( -r $jbosslogfile ){
	if ( ! -f $jbosslogfileorg ){
		my $foo=`cp $jbosslogfile $jbosslogfileorg`;
	}
}else{
	print "jboss logconfig not found ($jbosslogfileorg)";
	exit;
}

open(FD,"<$jbosslogfile");
open(FDx,">$jbosslogfilenew");

my $reminder=0;
while(<FD>){

	if( $reminder == 1 ){ 
		if( $_ =~ m/<priority value=".*" ><\/priority>/gio){	
			print "found level\n";
			$_ = "      <priority value=\"$level\" ></priority>\n";	
		}else{
			print "added level\n";
			print FDx "      <priority value=\"$level\" ></priority>\n";	
		}
	}

	if( $_ =~ m/<root>/gio){
		print "found root container\n";
		$reminder=1;		
	}else{
		$reminder=0;
	}

	print FDx $_;
}

close FD;
close FDx;

my $foo=`cp $jbosslogfilenew $jbosslogfile`;

